package com.gxdf.extendedcontrols.gxdfclient.components;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Pattern;

import com.gxdf.extendedcontrols.gxdfclient.data.DatePickerFragment;
import com.gxdf.extendedcontrols.gxdfclient.data.Field;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationMessage;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationResult;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationType;

import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.preference.EditTextPreference;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class GxdfDateField extends GxdfField implements OnClickListener, OnDateSetListener {
	
	TextView textView;
	TextView title;
	
	Context context;
	boolean isValid;

	public GxdfDateField(Context context, Field field) {
		super(context, field);
		this.context = context;
		this.setOrientation(VERTICAL);
		this.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
		
		textView = new EditText(this.context);
		textView.setText(getDefaultValue());
		textView.setFocusable(false);
		textView.setTextSize(22);
		textView.setId(999);
		textView.setOnClickListener(this);
		
		title = new TextView(this.context);
		title.setText(field.getLabelValue());
			
				
	}	

	
	private String getDefaultValue(){
		
		String value;
		
		if(field.getValues().size()==0){
			Calendar calendar =  Calendar.getInstance();
			CharSequence cs = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + "/" +  String.valueOf(calendar.get(Calendar.MONTH)+1) + "/" + String.valueOf(calendar.get(Calendar.YEAR));
			
			value = cs.toString();		
			
		}
		
		else{
			
			value = field.getValues().get(0);
		}
		
		return value;
	}
	
	private String getDefaultDate() {
		Date d = new Date();
		
		Calendar calendar =  Calendar.getInstance();
		CharSequence cs = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + "/" +  String.valueOf(calendar.get(Calendar.MONTH)+1) + "/" + String.valueOf(calendar.get(Calendar.YEAR));
		
		return cs.toString();
			
	}



	@Override
	public boolean errorFormat() {
		boolean invalid=true;
		
		if(getField().getFormatValidation().isEmpty()){
			invalid=false;
		}
		else{		
			//TODO: Ejecutar regexp. Si falla return true else return false	
			
			pattern = Pattern.compile(field.getFormatValidation());
			matcher = pattern.matcher(actualValues.get(0));
			invalid = matcher.matches();
		}
		
		return invalid;
	}

	@Override
	public boolean errorLength() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isFieldvalid() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void runValidationRules() {
		if(errorRequired()){
			isValid=false;
			ValidationResult vr = new ValidationResult(field.getName(),ValidationType.REQUIRED, ValidationMessage.getMessage(ValidationType.REQUIRED));
			validRules.add(vr);
		}
		
		if(errorFormat()){
			isValid=false;
			ValidationResult vr = new ValidationResult(field.getName(),ValidationType.FORMAT, ValidationMessage.getMessage(ValidationType.FORMAT));
			validRules.add(vr);			
		}

	}

	@Override
	public String getValue() {
		/*Date d = new Date();
		String cs = getField().getValues().get(0);
		if(cs.equals("")){
			
			Calendar calendar =  Calendar.getInstance();
			String cs2 = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + "/" +  String.valueOf(calendar.get(Calendar.MONTH)+1) + "/" + String.valueOf(calendar.get(Calendar.YEAR));
		
			return cs2;
		}else
			return cs;*/
		return this.textView.getText().toString();
	}

	@Override
	public void drawField() {
		
		this.addView(title);
		this.addView(textView);
		

	}

	@Override
	public void saveValues() {
		if(!textView.getText().toString().isEmpty()){
			
			List<String> newvalues = new ArrayList<String>();
			newvalues.add(0, textView.getText().toString());
			actualValues.clear();
			
			actualValues=newvalues;
		}
		else{
			actualValues.clear();
		}
	}

	@Override
	public boolean isModified() {
		return actualValues.equals(getField().getValues());
	}

	@Override
	public List<String> getActualValues() {
		return this.actualValues;
	}



	@Override
	public void onDateSet(DatePicker view, int year, int month, int dayDate) {
		Calendar c = new GregorianCalendar(year, month, dayDate);
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/M/yyyy");
		textView.setText(dateFormat.format(c.getTime()));
		
	}



	@Override
	public void onClick(View v) {
		if(v.getId() == 999){
			
			DatePickerFragment dpf = new DatePickerFragment(this, textView.getText().toString());
			FragmentActivity fa = (FragmentActivity) this.context;		
			
			dpf.show(fa.getSupportFragmentManager(), "datePicker");			
			
		}
		
	}

}
